package cn.edu.hubu.hubusmartmedicalsystem.service.impl;

import cn.edu.hubu.hubusmartmedicalsystem.entity.Patient;
import cn.edu.hubu.hubusmartmedicalsystem.mapper.PatientMapper;
import cn.edu.hubu.hubusmartmedicalsystem.service.PatientService;
import cn.edu.hubu.hubusmartmedicalsystem.vo.ResultVO;
import cn.hutool.crypto.digest.DigestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PatientServiceImpl implements PatientService {
    @Autowired
    private PatientMapper patientMapper;

    @Override
    public ResultVO checkAccount(String account) {
        Patient patient = patientMapper.selectByAccount(account);
        if (patient != null) {
            return new ResultVO<>(500, "账户已存在", account);
        } else {
            return new ResultVO<>(200, "账户可用", account);
        }
    }

    @Override
    public ResultVO savePatient(Patient patient) {
        if (patientMapper.selectByAccount(patient.getPatientId()) != null) {
            return new ResultVO<>(500, "注册失败，患者已存在", null);
        }
        String hashedPassword = DigestUtil.md5Hex(patient.getPatientPassword()); // 加密密码
        patient.setPatientPassword(hashedPassword);

        int result = patientMapper.insertPatient(patient);
        if (result > 0) {
            return new ResultVO<>(200, "患者注册成功", null);
        } else {
            return new ResultVO<>(500, "患者注册失败", null);
        }
    }

    @Override
    public ResultVO loginPatient(String account, String password) {
        Patient patient = patientMapper.selectByAccount(account);
        if (patient == null) {
            return new ResultVO<>(500, "账户不存在", null);
        }

        String hashedPassword = DigestUtil.md5Hex(password);
        if (!hashedPassword.equals(patient.getPatientPassword())) {
            return new ResultVO<>(500, "密码错误", null);
        }

        return new ResultVO<>(200, "登录成功", patient);
    }

    @Override
    public ResultVO<Patient> getPatientById(String patientId) {
        Patient patient = patientMapper.selectByID(patientId);
        if (patient != null) {
            return new ResultVO<>(200, "查询成功", patient);
        } else {
            return new ResultVO<>(500, "未找到该患者", null);
        }
    }

    @Override
    public ResultVO<List<Patient>> getAllPatients() {
        List<Patient> patients = patientMapper.selectAll();
        if (patients != null && !patients.isEmpty()) {
            return new ResultVO<>(200, "查询成功", patients);
        } else {
            return new ResultVO<>(500, "未找到患者信息", null);
        }
    }

    @Override
    public ResultVO<String> deleteById(String patientId) {
        int result = patientMapper.deleteById(patientId);
        if (result > 0) {
            return new ResultVO<>(200, "删除成功", null);
        } else {
            return new ResultVO<>(500, "删除失败，患者可能不存在", null);
        }
    }

    @Override
    public ResultVO<String> updatePatient(Patient patient) {
        Patient existingPatient = patientMapper.selectByID(patient.getPatientId());
        if (existingPatient == null) {
            return new ResultVO<>(500, "更新失败，患者不存在", null);
        }

        int result = patientMapper.updatePatient(patient);
        if (result > 0) {
            return new ResultVO<>(200, "更新成功", null);
        } else {
            return new ResultVO<>(500, "更新失败", null);
        }
    }
}
